<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="vue3简单使用"><meta name="keywords" content="vue3"><meta name="author" content="晟松"><meta name="copyright" content="晟松"><title>vue3简单使用 | 晟松</title><link rel="shortcut icon" href="/logo.png"><link rel="stylesheet" href="/css/index.css?version=1.9.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.9.0"><meta name="format-detection" content="telephone=no"><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="dns-prefetch" href="https://hm.baidu.com"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?c34d81d53341febe06a155941e2df16d";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();</script><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容:${query}"}},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  hexoVersion: '5.4.0'
} </script><meta name="generator" content="Hexo 5.4.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar" data-display="true"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-main-js"><span class="toc-number">1.</span> <span class="toc-text">1.main.js</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#vue2-x"><span class="toc-number">1.1.</span> <span class="toc-text">vue2.x</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#vue3-x"><span class="toc-number">1.2.</span> <span class="toc-text">vue3.x</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E7%BB%84%E5%90%88%E5%BC%8FAPI"><span class="toc-number">2.</span> <span class="toc-text">2.组合式API</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%9F%BA%E7%A1%80"><span class="toc-number">2.1.</span> <span class="toc-text">基础</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E8%AF%A6%E7%BB%86%E7%A4%BA%E4%BE%8B"><span class="toc-number">2.2.</span> <span class="toc-text">详细示例</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#setup-%E8%AF%A6%E8%A7%A3"><span class="toc-number">2.3.</span> <span class="toc-text">setup()详解</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%A8%A1%E6%9D%BF%E5%BC%95%E7%94%A8"><span class="toc-number">2.4.</span> <span class="toc-text">模板引用</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-%E6%80%BB%E7%BB%93%E5%BC%8F%E7%9A%84%E5%AE%8C%E6%95%B4%E6%A8%A1%E6%9D%BF"><span class="toc-number">3.</span> <span class="toc-text">3.总结式的完整模板</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-%E6%9B%B4%E4%BC%98%E9%9B%85%E7%9A%84%E4%BD%BF%E7%94%A8%E6%96%B9%E5%BC%8F"><span class="toc-number">4.</span> <span class="toc-text">4.更优雅的使用方式</span></a></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="/img/avatar.png"></div><div class="author-info__name text-center">晟松</div><div class="author-info__description text-center">晟松的博客</div><div class="follow-button"><a target="_blank" rel="noopener" href="https://gitee.com/cheng_song">Gitee</a></div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">52</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">45</span></a></div></div></div><div id="content-outer"><div id="top-container" style="background-image: url(/img/blog-bg.jpg)"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">晟松</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus">   <a class="site-page" href="/">Home</a><a class="site-page" href="/archives">Archives</a><a class="site-page" href="/tags">Tags</a><a class="site-page" href="/categories">Categories</a></span><span class="pull-right"><a class="site-page social-icon search"><i class="fa fa-search"></i><span> 搜索</span></a></span></div><div id="post-info"><div id="post-title">vue3简单使用</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2020-10-29</time><div class="post-meta-wordcount"><span>字数总计: </span><span class="word-count">3.1k</span><span class="post-meta__separator">|</span><span>阅读时长: 13 分钟</span></div></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><p>对vue3出现的一些新的语法方面的记录和简单使用</p>
<h4 id="1-main-js"><a href="#1-main-js" class="headerlink" title="1.main.js"></a>1.main.js</h4><p>首先vue2与vue3在main.js里面出现了一些改变</p>
<p>Vue 应用挂载到App.vue里面 <code>&lt;div id=&quot;app&quot;&gt;&lt;/div&gt;</code></p>
<h5 id="vue2-x"><a href="#vue2-x" class="headerlink" title="vue2.x"></a>vue2.x</h5><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> App <span class="keyword">from</span> <span class="string">&#x27;./App.vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> router <span class="keyword">from</span> <span class="string">&#x27;./router&#x27;</span></span><br><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">&#x27;./store&#x27;</span></span><br><span class="line"><span class="keyword">import</span> Vant <span class="keyword">from</span> <span class="string">&#x27;vant&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;vant/lib/index.css&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; Lazyload &#125; <span class="keyword">from</span> <span class="string">&#x27;vant&#x27;</span>;</span><br><span class="line"></span><br><span class="line">Vue.use(Lazyload)</span><br><span class="line">Vue.use(Vant)</span><br><span class="line">Vue.config.productionTip = <span class="literal">false</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue(&#123;</span><br><span class="line">  router,</span><br><span class="line">  store,</span><br><span class="line">  <span class="attr">render</span>: <span class="function"><span class="params">h</span> =&gt;</span> h(App)</span><br><span class="line">&#125;).$mount(<span class="string">&#x27;#app&#x27;</span>)</span><br></pre></td></tr></table></figure>

<h5 id="vue3-x"><a href="#vue3-x" class="headerlink" title="vue3.x"></a>vue3.x</h5><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; createApp &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> App <span class="keyword">from</span> <span class="string">&#x27;./App.vue&#x27;</span></span><br><span class="line"><span class="comment">//这里的App就是App.vue文件里面export导出的一个东西，之后挂载到&#x27;id=app&#x27;的div上</span></span><br><span class="line">createApp(App).mount(<span class="string">&#x27;#app&#x27;</span>) </span><br></pre></td></tr></table></figure>

<p>应用实例暴露当前全局 API 的子集，经验法则是，任何全局改变 Vue 行为的 API 现在都会移动到应用实例上，以下是当前全局 API 及其相应实例 API 的表：</p>
<table>
<thead>
<tr>
<th>2.x 全局 API</th>
<th>3.x 实例 API (<code>app</code>)</th>
</tr>
</thead>
<tbody><tr>
<td>Vue.config</td>
<td>app.config</td>
</tr>
<tr>
<td>Vue.config.productionTip</td>
<td><em>removed</em> (<a target="_blank" rel="noopener" href="https://www.vue3js.cn/docs/zh/guide/migration/global-api.html#config-productiontip-removed">见下方</a>)</td>
</tr>
<tr>
<td>Vue.config.ignoredElements</td>
<td>app.config.isCustomElement (<a target="_blank" rel="noopener" href="https://www.vue3js.cn/docs/zh/guide/migration/global-api.html#config-ignoredelements-is-now-config-iscustomelement">见下方</a>)</td>
</tr>
<tr>
<td>Vue.component</td>
<td>app.component</td>
</tr>
<tr>
<td>Vue.directive</td>
<td>app.directive</td>
</tr>
<tr>
<td>Vue.mixin</td>
<td>app.mixin</td>
</tr>
<tr>
<td>Vue.use</td>
<td>app.use (<a target="_blank" rel="noopener" href="https://www.vue3js.cn/docs/zh/guide/migration/global-api.html#a-note-for-plugin-authors">见下方</a>)</td>
</tr>
</tbody></table>
<p>所有其他不全局改变行为的全局 API 现在被命名为 exports</p>
<h4 id="2-组合式API"><a href="#2-组合式API" class="headerlink" title="2.组合式API"></a>2.组合式API</h4><p>用组件的选项 (<code>data</code>、<code>computed</code>、<code>methods</code>、<code>watch</code>) 组织逻辑在大多数情况下都有效。然而，当我们的组件变得更大时，<strong>逻辑关注点</strong>的列表也会增长。这可能会导致组件难以阅读和理解，尤其是对于那些一开始就没有编写这些组件的人来说。</p>
<p>一个大型组件的示例，其中<strong>逻辑关注点</strong>是按颜色分组。</p>
<p>这种碎片化使得理解和维护复杂组件变得困难。选项的分离掩盖了潜在的逻辑问题。此外，在处理单个逻辑关注点时，我们必须不断地“跳转”相关代码的选项块。</p>
<p>如果我们能够将与同一个逻辑关注点相关的代码配置在一起会更好。而这正是组合式 API 使我们能够做到的。</p>
<h5 id="基础"><a href="#基础" class="headerlink" title="基础"></a>基础</h5><p>开始使用组合式 API，我们首先需要一个可以实际使用它的地方，也称入口函数。在 Vue 组件中，我们将此位置称为 <code>setup</code></p>
<p>新的 <code>setup</code> 组件选项在<strong>创建组件之前</strong>执行，一旦 <code>props</code> 被解析，并充当合成 API 的入口点。</p>
<p>setup 函数<strong>会在 beforeCreate之前执行</strong>, vue3也是取消了这两个钩子，统一用setup代替, 该函数相当于一个生命周期函数，vue中过去的data，methods，watch等全部都用对应的新增api写在setup()函数中，也可不写在这里面。</p>
<ul>
<li>props: 用来接收 props 数据</li>
<li>context 用来定义上下文, 上下文对象中包含了一些有用的属性，这些属性在 vue 2.x 中需要通过 this 才能访问到, 在 setup() 函数中无法访问到 this，是个 undefined</li>
<li>返回值: return {}, 返回响应式数据, 模版中需要使用的函数</li>
</ul>
<hr>
<p><strong>WARNING：</strong></p>
<p>由于在执行 <code>setup</code> 时尚未创建组件实例，因此在 <code>setup</code> 选项中没有 <code>this</code>。这意味着，<strong>除了 <code>props</code> 之外</strong>，你将无法访问组件中声明的任何属性——<strong>本地状态</strong>、<strong>计算属性</strong>或<strong>方法</strong>。</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="attr">components</span>: &#123; RepositoriesFilters, RepositoriesSortBy, RepositoriesList &#125;,</span><br><span class="line">  <span class="attr">props</span>: &#123;</span><br><span class="line">    <span class="attr">user</span>: &#123; <span class="attr">type</span>: <span class="built_in">String</span> &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="comment">//在这里面其实也可以使用vue2的一些属性方法和钩子函数</span></span><br><span class="line">  <span class="function"><span class="title">data</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">      <span class="keyword">return</span>&#123;</span><br><span class="line">          <span class="attr">mydata</span>:&#123;&#125;</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="function"><span class="title">setup</span>(<span class="params">props,context</span>)</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(props) <span class="comment">// &#123; user: &#x27;&#x27; &#125;</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> &#123;&#125; <span class="comment">// 这里返回的任何内容都可以用于组件的其余部分</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 组件的“其余部分”</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在 Vue 3.0 中，我们可以通过一个新的 <code>ref</code> 函数使任何响应式变量在任何地方起作用</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; ref &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> counter = ref(<span class="number">0</span>)<span class="comment">//注意返回的是一个对象，里面的value才是我们传入的值</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(counter) <span class="comment">// &#123; value: 0 &#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(counter.value) <span class="comment">// 0</span></span><br><span class="line"></span><br><span class="line">counter.value++</span><br><span class="line"><span class="built_in">console</span>.log(counter.value) <span class="comment">// 1</span></span><br></pre></td></tr></table></figure>

<h5 id="详细示例"><a href="#详细示例" class="headerlink" title="详细示例"></a>详细示例</h5><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">&lt;p @click = &quot;handleclick&quot;&gt;</span><br><span class="line">    count:&#123;&#123;count&#125;&#125;</span><br><span class="line">    state:&#123;&#123;state.timer&#125;&#125;</span><br><span class="line">    double:&#123;&#123;double&#125;&#125;</span><br><span class="line">    &lt;/p&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">    //组合式api的引入，几乎所有的vue2属性都可以引入</span><br><span class="line">    import &#123;ref,reactive,readonly,computed,watch&#125; from &#x27;vue&#x27;</span><br><span class="line">    export default &#123;</span><br><span class="line">        //注意这个setup()函数，vue3的重点</span><br><span class="line">        setup()&#123;</span><br><span class="line">            //第一种响应式的方式ref	值类型string number </span><br><span class="line">            const count  = ref(0);	//创建一个响应式数据对象，以vue3的方式</span><br><span class="line">            const handleclick = ()=&gt;&#123;</span><br><span class="line">                count.value++;	//因为现在的count变成了一个对象了，需要控制的是其中的value</span><br><span class="line">                state.timer++;</span><br><span class="line">            &#125;</span><br><span class="line">            //第二种响应式的方式reactive	引用类型&#123;&#125; []</span><br><span class="line">            const state  = reactive(&#123;timer:0&#125;);	//创建出来是一个proxy对象</span><br><span class="line">            //readonly格式类似reactive，但是只能初始化进行读取，无法进行更改</span><br><span class="line">            </span><br><span class="line">            //响应式系统api</span><br><span class="line">            const double = computed(()=&gt;&#123;</span><br><span class="line">                return count.value * 2</span><br><span class="line">            &#125;);</span><br><span class="line">            watch(double,(value)=&gt;&#123;</span><br><span class="line">                console.log(value)</span><br><span class="line">            &#125;);</span><br><span class="line">            //注意所有的变量包括函数都需要返回</span><br><span class="line">            return &#123;</span><br><span class="line">                count,</span><br><span class="line">                handleclick,</span><br><span class="line">                state,</span><br><span class="line">                double</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<p>同时也可以将组合式API定义为一个函数，之后在其他的页面import一下</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// src/composables/useUserRepositories.js</span></span><br><span class="line"><span class="comment">//函数式组合API</span></span><br><span class="line"><span class="keyword">import</span> &#123; fetchUserRepositories &#125; <span class="keyword">from</span> <span class="string">&#x27;@/api/repositories&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123; ref, onMounted, watch &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span> <span class="title">useUserRepositories</span>(<span class="params">user</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> repositories = ref([])</span><br><span class="line">  <span class="keyword">const</span> getUserRepositories = <span class="keyword">async</span> () =&gt; &#123;</span><br><span class="line">    repositories.value = <span class="keyword">await</span> fetchUserRepositories(user.value)</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  onMounted(getUserRepositories)</span><br><span class="line">  watch(user, getUserRepositories)</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    repositories,</span><br><span class="line">    getUserRepositories</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//使用函数式组合API</span></span><br><span class="line"><span class="keyword">import</span> 	useUserRepositories <span class="keyword">from</span> <span class="string">&#x27;@/composables/useUserRepositories&#x27;</span></span><br><span class="line"><span class="keyword">import</span> useRepositoryNameSearch <span class="keyword">from</span> <span class="string">&#x27;@/composables/useRepositoryNameSearch&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123; toRefs &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="attr">components</span>: &#123; RepositoriesFilters, RepositoriesSortBy, RepositoriesList &#125;,</span><br><span class="line">  <span class="attr">props</span>: &#123;</span><br><span class="line">    <span class="attr">user</span>: &#123; <span class="attr">type</span>: <span class="built_in">String</span> &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  setup (props) &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; user &#125; = toRefs(props)</span><br><span class="line">	<span class="comment">//通过解构去除函数内部经过处理的值，可以直接进行使用</span></span><br><span class="line">    <span class="keyword">const</span> &#123; repositories, getUserRepositories &#125; = useUserRepositories(user)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">const</span> &#123;</span><br><span class="line">      searchQuery,</span><br><span class="line">      repositoriesMatchingSearchQuery&#125; = useRepositoryNameSearch(repositories)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      <span class="comment">// 因为我们并不关心未经过滤的仓库</span></span><br><span class="line">      <span class="comment">// 我们可以在 `repositories` 名称下暴露过滤后的结果</span></span><br><span class="line">      <span class="attr">repositories</span>: repositoriesMatchingSearchQuery,</span><br><span class="line">      getUserRepositories,</span><br><span class="line">      searchQuery,</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  data () &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      <span class="attr">filters</span>: &#123; ... &#125;, <span class="comment">// 3</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">computed</span>: &#123;</span><br><span class="line">    filteredRepositories () &#123; ... &#125;, <span class="comment">// 3</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">methods</span>: &#123;</span><br><span class="line">    updateFilters () &#123; ... &#125;, <span class="comment">// 3</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="setup-详解"><a href="#setup-详解" class="headerlink" title="setup()详解"></a>setup()详解</h5><p>使用 <code>setup</code> 函数时，它将接受两个参数：</p>
<ol>
<li><code>props</code></li>
<li><code>context</code></li>
</ol>
<p><code>setup</code> 函数中的第一个参数是 <code>props</code>。正如在一个标准组件中所期望的那样，<code>setup</code> 函数中的 <code>props</code> 是响应式的，当传入新的 prop 时，它将被更新</p>
<p>如果需要解构 prop，可以通过使用 <code>setup</code> 函数中的 <a target="_blank" rel="noopener" href="https://www.vue3js.cn/docs/zh/guide/reactivity-fundamentals.html#%E5%93%8D%E5%BA%94%E5%BC%8F%E7%8A%B6%E6%80%81%E8%A7%A3%E6%9E%84"><code>toRefs</code></a> 来安全地完成此操作。</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// MyBook.vue</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123; toRefs &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="title">setup</span>(<span class="params">props</span>)</span> &#123;</span><br><span class="line">	<span class="keyword">const</span> &#123; title &#125; = toRefs(props)</span><br><span class="line"></span><br><span class="line">	<span class="built_in">console</span>.log(title.value)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>传递给 <code>setup</code> 函数的第二个参数是 <code>context</code>。<code>context</code> 是一个普通的 JavaScript 对象，它暴露三个组件的 property：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// MyBook.vue</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="comment">//context可以被解构为&#123; attrs, slots, emit &#125;的样子</span></span><br><span class="line">  <span class="function"><span class="title">setup</span>(<span class="params">props, context</span>)</span> &#123;</span><br><span class="line">    <span class="comment">// Attribute (非响应式对象)</span></span><br><span class="line">    <span class="built_in">console</span>.log(context.attrs)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 插槽 (非响应式对象)</span></span><br><span class="line">    <span class="built_in">console</span>.log(context.slots)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 触发事件 (方法)</span></span><br><span class="line">    <span class="built_in">console</span>.log(context.emit)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>执行 <code>setup</code> 时，组件实例尚未被创建。因此，你只能访问以下 property：</p>
<ul>
<li><code>props</code></li>
<li><code>attrs</code></li>
<li><code>slots</code></li>
<li><code>emit</code></li>
</ul>
<p>换句话说，你<strong>将无法访问</strong>以下组件选项：</p>
<ul>
<li><code>data</code></li>
<li><code>computed</code></li>
<li><code>methods</code></li>
</ul>
<p><code>setup</code> 还可以返回一个渲染函数，该函数可以直接使用在同一作用域中声明的响应式状态：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// MyBook.vue</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123; h, ref, reactive &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="function"><span class="title">setup</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> readersNumber = ref(<span class="number">0</span>)</span><br><span class="line">    <span class="keyword">const</span> book = reactive(&#123; <span class="attr">title</span>: <span class="string">&#x27;Vue 3 Guide&#x27;</span> &#125;)</span><br><span class="line">    <span class="comment">// Please note that we need to explicitly expose ref value here</span></span><br><span class="line">    <span class="keyword">return</span> <span class="function">() =&gt;</span> h(<span class="string">&#x27;div&#x27;</span>, [readersNumber.value, book.title])</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>在 <code>setup()</code> 内部，<code>this</code> 不会是该活跃实例的引用</strong>，因为 <code>setup()</code> 是在解析其它组件选项之前被调用的，所以 <code>setup()</code> 内部的 <code>this</code> 的行为与其它选项中的 <code>this</code> 完全不同。这在和其它选项式 API 一起使用 <code>setup()</code> 时可能会导致混淆。</p>
<p>在setup()里面使用provide和inject也是通过import引入然后在setup里面进行操作使用的</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  <span class="xml"><span class="tag">&lt;<span class="name">MyMarker</span> /&gt;</span></span></span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span></span><br><span class="line"><span class="xml">import &#123; provide,ref &#125; from &#x27;vue&#x27;</span></span><br><span class="line"><span class="xml">import MyMarker from &#x27;./MyMarker.vue</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">export default &#123;</span></span><br><span class="line"><span class="xml">  components: &#123;</span></span><br><span class="line"><span class="xml">    MyMarker</span></span><br><span class="line"><span class="xml">  &#125;,</span></span><br><span class="line"><span class="xml">  setup() &#123;</span></span><br><span class="line"><span class="xml">    //也可进行响应式的变化property</span></span><br><span class="line"><span class="xml">    const refval = ref(12)</span></span><br><span class="line"><span class="xml">    provide(&#x27;refval&#x27;, refval)</span></span><br><span class="line"><span class="xml">      </span></span><br><span class="line"><span class="xml">    provide(&#x27;location&#x27;, &#x27;North Pole&#x27;)</span></span><br><span class="line"><span class="xml">    provide(&#x27;geolocation&#x27;, &#123;</span></span><br><span class="line"><span class="xml">      longitude: 90,</span></span><br><span class="line"><span class="xml">      latitude: 135</span></span><br><span class="line"><span class="xml">    &#125;)</span></span><br><span class="line"><span class="xml">  &#125;</span></span><br><span class="line"><span class="xml">&#125;</span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span></span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">&lt;!-- src/components/MyMarker.vue --&gt;</span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span></span><br><span class="line"><span class="javascript"><span class="xml"><span class="keyword">import</span> &#123; inject &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span></span></span><br><span class="line"><span class="javascript"><span class="xml"></span></span></span><br><span class="line"><span class="javascript"><span class="xml"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">  <span class="function"><span class="title">setup</span>(<span class="params"></span>)</span> &#123;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">    <span class="keyword">const</span> userLocation = inject(<span class="string">&#x27;location&#x27;</span>, <span class="string">&#x27;The Universe&#x27;</span>)<span class="comment">//后面这个是默认的值</span></span></span></span><br><span class="line"><span class="javascript"><span class="xml">    <span class="keyword">const</span> userGeolocation = inject(<span class="string">&#x27;geolocation&#x27;</span>)</span></span></span><br><span class="line"><span class="javascript"><span class="xml"></span></span></span><br><span class="line"><span class="javascript"><span class="xml">    <span class="keyword">return</span> &#123;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">      userLocation,</span></span></span><br><span class="line"><span class="javascript"><span class="xml">      userGeolocation</span></span></span><br><span class="line"><span class="javascript"><span class="xml">    &#125;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">  &#125;</span></span></span><br><span class="line"><span class="javascript"><span class="xml">&#125;</span></span></span><br><span class="line"><span class="javascript"><span class="xml"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span></span><br></pre></td></tr></table></figure>

<h5 id="模板引用"><a href="#模板引用" class="headerlink" title="模板引用"></a>模板引用</h5><p>在使用组合式 API 时，<a target="_blank" rel="noopener" href="https://www.vue3js.cn/docs/zh/guide/reactivity-fundamentals.html#creating-standalone-reactive-values-as-refs">响应式引用</a>和<a target="_blank" rel="noopener" href="https://www.vue3js.cn/docs/zh/guide/component-template-refs.html">模板引用</a>的概念是统一的。为了获得对模板内元素或组件实例的引用，我们可以像往常一样声明 ref 并从 <a target="_blank" rel="noopener" href="https://www.vue3js.cn/docs/zh/guide/composition-api-setup.html">setup()</a> 返回：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span> </span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">ref</span>=<span class="string">&quot;root&quot;</span>&gt;</span>注意这里的ref属性 This is a root element<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">  <span class="keyword">import</span> &#123; ref, onMounted &#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">  <span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="title">setup</span>(<span class="params"></span>)</span> &#123;</span></span><br><span class="line"><span class="javascript">      <span class="comment">//这里获取template里面的ref绑定的div，之前是通过this.$refs获取到这个div</span></span></span><br><span class="line"><span class="javascript">      <span class="keyword">const</span> root = ref(<span class="literal">null</span>)</span></span><br><span class="line"><span class="javascript">      onMounted(<span class="function">() =&gt;</span> &#123;</span></span><br><span class="line"><span class="javascript">        <span class="comment">// DOM元素将在初始渲染后分配给ref</span></span></span><br><span class="line"><span class="javascript">        <span class="built_in">console</span>.log(root.value) <span class="comment">// &lt;div&gt;这是根元素&lt;/div&gt;</span></span></span><br><span class="line"><span class="javascript">      &#125;)</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">      <span class="keyword">return</span> &#123;</span></span><br><span class="line"><span class="javascript">        root</span></span><br><span class="line"><span class="javascript">      &#125;</span></span><br><span class="line"><span class="javascript">    &#125;</span></span><br><span class="line"><span class="javascript">  &#125;</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>这里我们在渲染上下文中暴露 <code>root</code>，并通过 <code>ref=&quot;root&quot;</code>，将其绑定到 div 作为其 ref。在虚拟 DOM 补丁算法中，如果 VNode 的 <code>ref</code> 键对应于渲染上下文中的 ref，则 VNode 的相应元素或组件实例将被分配给该 ref 的值。这是在虚拟 DOM 挂载/打补丁过程中执行的，因此模板引用只会在初始渲染之后获得赋值。</p>
<p>作为模板使用的 ref 的行为与任何其他 ref 一样：它们是响应式的，可以传递到 (或从中返回) 复合函数中</p>
<h4 id="3-总结式的完整模板"><a href="#3-总结式的完整模板" class="headerlink" title="3.总结式的完整模板"></a>3.总结式的完整模板</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  <span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;mine&quot;</span> <span class="attr">ref</span>=<span class="string">&quot;elmRefs&quot;</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">span</span>&gt;</span>&#123;&#123; name &#125;&#125;<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">br</span> /&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">span</span>&gt;</span>&#123;&#123; count &#125;&#125;<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="xml">      <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;handleClick&quot;</span>&gt;</span>测试按钮<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="xml">      <span class="tag">&lt;<span class="name">li</span> <span class="attr">v-for</span>=<span class="string">&quot;item in list&quot;</span> <span class="attr">:key</span>=<span class="string">&quot;item.id&quot;</span>&gt;</span>&#123;&#123; item.name &#125;&#125;<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="xml">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script lang=&quot;ts&quot;&gt;</span><br><span class="line">import &#123;</span><br><span class="line">  computed,</span><br><span class="line">  defineComponent,</span><br><span class="line">  getCurrentInstance,</span><br><span class="line">  onMounted,</span><br><span class="line">  PropType,</span><br><span class="line">  reactive,</span><br><span class="line">  ref,</span><br><span class="line">  toRefs,</span><br><span class="line">&#125; from &quot;vue&quot;;</span><br><span class="line"></span><br><span class="line">interface IState &#123;</span><br><span class="line">  count: 0;</span><br><span class="line">  name: string;</span><br><span class="line">  list: Array&lt;object&gt;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">export default defineComponent(&#123;</span><br><span class="line">  name: &quot;demo&quot;,</span><br><span class="line">  // 父组件传子组件参数</span><br><span class="line">  props: &#123;</span><br><span class="line">    name: &#123;</span><br><span class="line">      type: String as PropType&lt;null | &quot;&quot;&gt;,</span><br><span class="line">      default: &quot;vue3.x&quot;,</span><br><span class="line">    &#125;,</span><br><span class="line">    list: &#123;</span><br><span class="line">      type: Array as PropType&lt;object[]&gt;,</span><br><span class="line">      default: () =&gt; [],</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;,</span><br><span class="line">  components: &#123;</span><br><span class="line">    /// TODO 组件注册</span><br><span class="line">  &#125;,</span><br><span class="line">  emits: [&quot;emits-name&quot;], // 为了提示作用</span><br><span class="line">  setup(props, context) &#123;</span><br><span class="line">    console.log(props.name);</span><br><span class="line">    console.log(props.list);</span><br><span class="line"></span><br><span class="line">    const state = reactive&lt;IState&gt;(&#123;</span><br><span class="line">      name: &quot;vue 3.0 组件&quot;,</span><br><span class="line">      count: 0,</span><br><span class="line">      list: [</span><br><span class="line">        &#123;</span><br><span class="line">          name: &quot;vue&quot;,</span><br><span class="line">          id: 1,</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">          name: &quot;vuex&quot;,</span><br><span class="line">          id: 2,</span><br><span class="line">        &#125;,</span><br><span class="line">      ],</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    const a = computed(() =&gt; state.name);</span><br><span class="line"></span><br><span class="line">    onMounted(() =&gt; &#123;&#125;);</span><br><span class="line"></span><br><span class="line">    function handleClick() &#123;</span><br><span class="line">      state.count++;</span><br><span class="line">      // 调用父组件的方法</span><br><span class="line">      context.emit(&quot;emits-name&quot;, state.count);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    return &#123;</span><br><span class="line">      ...toRefs(state),</span><br><span class="line">      handleClick,</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;);</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<h4 id="4-更优雅的使用方式"><a href="#4-更优雅的使用方式" class="headerlink" title="4.更优雅的使用方式"></a>4.更优雅的使用方式</h4><p>简单的说就是将一个相关的逻辑都放在一个函数里面，然后将这个函数在**setup()**里面调用，并解构出函数里面return出的一些函数和对象等数据，然后统一在 <strong>setup()</strong> 里面return出来，之后就正常进行使用</p>
<p><a target="_blank" rel="noopener" href="https://gitee.com/cheng_song/shirakami-haruka-button">vue3+ts 示例</a></p>
<p>主要使用示例是在 home.vue 和harukaButton.vue里面</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">    &lt;v-card</span><br><span class="line">        flat</span><br><span class="line">        rounded=&quot;xl&quot;</span><br><span class="line">        class=&quot;haruka-card&quot;</span><br><span class="line">    &gt;</span><br><span class="line">        &lt;v-card-title v-if=&quot;title&quot; primary-title&gt;</span><br><span class="line">            &#123;&#123; title &#125;&#125;</span><br><span class="line">        &lt;/v-card-title&gt;</span><br><span class="line">        &lt;v-card-text&gt;</span><br><span class="line">            &lt;slot /&gt;</span><br><span class="line">        &lt;/v-card-text&gt;</span><br><span class="line">    &lt;/v-card&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script lang=&quot;ts&quot;&gt;</span><br><span class="line">import &#123; computed, defineComponent, Ref, toRefs &#125; from &#x27;@vue/composition-api&#x27;</span><br><span class="line">import i18n from &#x27;@/plugins/i18n&#x27;</span><br><span class="line"></span><br><span class="line">function useCardTitle(tag: Ref&lt;string&gt;, rawTitle?: Ref&lt;string&gt;) &#123;</span><br><span class="line">    const title = computed(() =&gt; &#123;</span><br><span class="line">        if (rawTitle?.value) &#123;</span><br><span class="line">            return rawTitle.value</span><br><span class="line">        &#125;</span><br><span class="line">        if (!tag.value) &#123;</span><br><span class="line">            return &#x27;&#x27;</span><br><span class="line">        &#125;</span><br><span class="line">        return i18n.t(`tags.$&#123;tag.value&#125;`)</span><br><span class="line">    &#125;)</span><br><span class="line">    return title</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">export default defineComponent(&#123;</span><br><span class="line">    name: &#x27;HarukaCard&#x27;,</span><br><span class="line">    props: &#123;</span><br><span class="line">        tag: &#123;</span><br><span class="line">            type: String,</span><br><span class="line">            default: &#x27;&#x27;,</span><br><span class="line">        &#125;,</span><br><span class="line">        rawTitle: &#123;</span><br><span class="line">            type: String,</span><br><span class="line">            default: &#x27;&#x27;,</span><br><span class="line">        &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">    setup(props, ctx) &#123;</span><br><span class="line">        const &#123; rawTitle, tag &#125; = toRefs(props)</span><br><span class="line">        // 就像这样调用这个函数</span><br><span class="line">        const title = useCardTitle(tag, rawTitle)</span><br><span class="line">        return &#123;</span><br><span class="line">            title,</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">&#125;)</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style lang=&quot;scss&quot; scoped&gt;</span><br><span class="line">@import &#x27;@/styles/index.scss&#x27;;</span><br><span class="line"></span><br><span class="line">.haruka-card &#123;</span><br><span class="line">    box-shadow: 1px 5px 22px rgb(255, 174, 166, 0.6) !important;</span><br><span class="line">&#125;</span><br><span class="line">&lt;/style&gt;</span><br></pre></td></tr></table></figure>

</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">晟松</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://chengsong.info/2020/10/29/vue3简单使用/">https://chengsong.info/2020/10/29/vue3简单使用/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://chengsong.info">晟松</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/vue3/">vue3</a></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2020/11/16/vue%E6%BA%90%E7%A0%81%E7%AE%80%E5%8D%95%E8%A7%A3%E6%9E%90/"><i class="fa fa-chevron-left">  </i><span>vue源码简单解析</span></a></div><div class="next-post pull-right"><a href="/2020/10/27/vue3%E6%96%B0%E7%89%B9%E6%80%A7/"><span>vue3新特性</span><i class="fa fa-chevron-right"></i></a></div></nav></div></div><footer class="footer-bg" style="background-image: url(/img/blog-bg.jpg)"><div class="layout" id="footer"><div class="copyright">&copy;2020 - 2023 By 晟松</div><div class="framework-info"><span>驱动 - </span><a target="_blank" rel="noopener" href="http://hexo.io"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a target="_blank" rel="noopener" href="https://github.com/Molunerfinn/hexo-theme-melody"><span>Melody</span></a></div><div class="icp"><a target="_blank" rel="noopener" href="http://www.beian.miit.gov.cn/"><span>湘ICP备2020021729号</span></a></div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv"><i class="fa fa-file"></i><span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.9.0"></script><script src="/js/fancybox.js?version=1.9.0"></script><script src="/js/sidebar.js?version=1.9.0"></script><script src="/js/copy.js?version=1.9.0"></script><script src="/js/fireworks.js?version=1.9.0"></script><script src="/js/transition.js?version=1.9.0"></script><script src="/js/scroll.js?version=1.9.0"></script><script src="/js/head.js?version=1.9.0"></script><script src="/js/search/local-search.js"></script><script>if(/Android|webOS|iPhone|iPod|iPad|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
  $('#top-container').addClass('is-mobile')
}</script><div class="search-dialog" id="local-search"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章"></div></div></div><hr><div id="local-search-results"><div id="local-hits"></div><div id="local-stats"><div class="local-search-stats__hr" id="hr"><span>由</span> <a target="_blank" rel="noopener" href="https://github.com/wzpan/hexo-generator-search" style="color:#49B1F5;">hexo-generator-search</a>
 <span>提供支持</span></div></div></div><span class="search-close-button"><i class="fa fa-times"></i></span></div><div class="search-mask"></div></body></html>